package se_dsp_adv_creative

import (
	"fmt"
	"strconv"
	"strings"
	"time"
	"wx-api/dsp-api/cache"
	"wx-api/dsp-api/errmsg"
	"wx-api/dsp-api/handlers"
	"wx-api/dsp-api/model/m_dsp_adv_creative"
	"wx-api/dsp-api/services/comm"
	"wx-api/dsp-api/utils"
)

func List(c *handlers.Controller) {
	id := c.GetString("id")
	name := c.GetString("name")
	flag := c.GetInt32("flag", 0)
	status := c.GetInt32("status", 1)
	pageNum, pageSize := c.GetPage()
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_dsp_adv_creative.List(id, name, "", flag, status, pageNum, pageSize)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("select list is error"))
		return
	}
	tmplist := []m_dsp_adv_creative.DspAdvCreative{}
	ttt := data.Data.(*[]m_dsp_adv_creative.DspAdvCreative)
	for _, v := range *ttt {
		tmp := v
		sinfo, ok := cache.CacheDspAdvPlanMapNGet(v.PlanId)
		if ok {
			tmp.PlanName = sinfo.Name
		} else {
			tmp.PlanName = "-"
		}
		app_info, ok := cache.CacheDspAdvAppMapNGet(v.AppId)
		if ok {
			tmp.AppName = app_info.Name
		} else {
			tmp.AppName = "-"
		}
		tmplist = append(tmplist, tmp)
	}
	data.Data = tmplist
	c.SetErrorMsg(data, errmsg.SuccessMsg())
	return
}

func Add(c *handlers.Controller) {
	var form m_dsp_adv_creative.DspAdvCreative
	if err := c.Ctx.ShouldBind(&form); err != nil {
		fmt.Println(err)
		c.BadRequest()
		return
	}
	utils.CleanStruct(&form)
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	if form.PlanId == "" {
		c.SetErrorMsg(nil, errmsg.GetError("无所属计划"))
		return
	}
	p_info, ok := cache.CacheDspAdvPlanMapNGet(form.PlanId)
	if !ok || p_info == nil {
		c.SetErrorMsg(nil, errmsg.GetError("所属计划不存在"))
		return
	}
	if len(form.VideoAdm) == 0 && len(form.ImgAdm) == 0 {
		c.SetErrorMsg(nil, errmsg.GetError("无广告素材"))
		return
	}
	if len(form.Title) == 0 {
		c.SetErrorMsg(nil, errmsg.GetError("无广告文案"))
		return
	}
	if form.Budget == 0 {
		c.SetErrorMsg(nil, errmsg.GetError("无广告预算"))
		return
	}
	if form.Price == 0 {
		c.SetErrorMsg(nil, errmsg.GetError("无广告出价"))
		return
	}
	if form.Link == "" {
		c.SetErrorMsg(nil, errmsg.GetError("无广告落地页"))
		return
	}
	form.AdvId = p_info.AdvId
	form.Code = "1320" + strconv.FormatInt(time.Now().UnixNano()/1e6, 10)
	id, err := m_dsp_adv_creative.Save(form)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("insert is error"))
		return
	}
	go cache.CacheDspAdvCreativeMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Edit(c *handlers.Controller) {
	var form m_dsp_adv_creative.DspAdvCreative
	if err := c.Ctx.ShouldBind(&form); err != nil {
		fmt.Println(err)
		c.BadRequest()
		return
	}
	utils.CleanStruct(&form)
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	adxinfo, err := m_dsp_adv_creative.GetByCk(form.Id.Hex())
	if err != nil || adxinfo == nil {
		c.SetErrorMsg(nil, errmsg.GetError("data is empty"))
		return
	}
	if len(form.VideoAdm) == 0 && len(form.ImgAdm) == 0 {
		c.SetErrorMsg(nil, errmsg.GetError("无广告素材"))
		return
	}
	if len(form.Title) == 0 {
		c.SetErrorMsg(nil, errmsg.GetError("无广告文案"))
		return
	}
	if form.Budget == 0 {
		c.SetErrorMsg(nil, errmsg.GetError("无广告预算"))
		return
	}
	if form.Price == 0 {
		c.SetErrorMsg(nil, errmsg.GetError("无广告出价"))
		return
	}
	if form.Link == "" {
		c.SetErrorMsg(nil, errmsg.GetError("无广告落地页"))
		return
	}
	err = m_dsp_adv_creative.Update(form)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("edit is error"))
		return
	}
	go cache.CacheDspAdvCreativeMapUpdate(adxinfo.Id.Hex())
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

type SelEditParam struct {
	Ids    []string `json:"ids"`
	Price  float64  `json:"price"`
	Link   string   `json:"link"`
	AppId  string   `json:"app_id"`
	Dplink string   `json:"dplink"`
	Unlink string   `json:"unlink"`
}

func SelEdit(c *handlers.Controller) {
	var form SelEditParam
	if err := c.Ctx.ShouldBind(&form); err != nil {
		fmt.Println(err)
		c.BadRequest()
		return
	}
	utils.CleanStruct(&form)
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	if len(form.Ids) == 0 {
		c.SetErrorMsg(nil, errmsg.GetError("data is empty"))
		return
	}
	for _, id := range form.Ids {
		_ = m_dsp_adv_creative.SelUpdate(id, form.Price, form.Link, form.AppId, form.Dplink, form.Unlink)
		go cache.CacheDspAdvCreativeMapUpdate(id)
	}

	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Info(c *handlers.Controller) {
	id := c.GetString("id")
	if id == "" {
		c.SetErrorMsg(nil, errmsg.GetError("必填参数为空"))
		return
	}
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_dsp_adv_creative.GetByCk(id)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	c.SetErrorMsg(data, errmsg.SuccessMsg())
	return
}

func Flag(c *handlers.Controller) {
	id := c.GetString("id")
	flag := c.GetInt32("flag", 0)
	if id == "" || (flag != 1 && flag != 2) {
		c.SetErrorMsg(nil, errmsg.GetError("非法参数"))
		return
	}
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_dsp_adv_creative.GetByCk(id)
	if err != nil || data == nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	err = m_dsp_adv_creative.UpdateFalg(id, flag)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("flag is error"))
		return
	}
	go cache.CacheDspAdvCreativeMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Stats(c *handlers.Controller) {
	id := c.GetString("id")
	flag := c.GetInt32("status", 0)
	if id == "" || (flag != 1 && flag != 2) {
		c.SetErrorMsg(nil, errmsg.GetError("非法参数"))
		return
	}
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_dsp_adv_creative.GetByCk(id)
	if err != nil || data == nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	err = m_dsp_adv_creative.UpdateDel(id, flag)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("stats is error"))
		return
	}
	go cache.CacheDspAdvCreativeMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func All(c *handlers.Controller) {
	id := c.GetString("ids")
	ids := strings.Split(id, ",")
	ids = utils.UnqArrs(ids)
	data := cache.CacheDspAdvCreativeMapAll(ids)
	c.SetErrorMsg(data, errmsg.SuccessMsg())
	return
}
